/**
 * Window with tab control and set of tab panes.
 *
 * @author Andrey Anisimov <hercules@inbox.ru>
 * @see core.ui.TabBar
 */

import core.ui.Window;
import core.ui.TabBar;
import core.ui.CompositeWindow;

class TabWindow extends CompositeWindow {
	
	/**
	 * Creates new tab window.
	 * 
	 * @param parent (Window) [optional] parent window
	 * @param x, y (int) [optional] coordinates
	 * @param width, height (int) [optional] window size.
	 */	
	this.TabWindow = function(parent, x, y, width, height) {
		super(parent, x, y, width, height);

		this.panes = [];

		this.tabBar = new TabBar(this);
		this.tabBar.setStyle('z-index', 2);
		this.tabBar.addTabListener(this);
		
		this.contentPane = new Window(this);
		this.contentPane.setStyle('z-index', 1);
		this.contentPane.setStyleName('TabContentPane');
		this.contentPane.setStyle('overflow', 'visible');
		
		this.updateLayout();
	}

	/**
	 * Adds new tab pane.
	 *
	 * @param pane (Window) tab pane.
	 * @param text (string) tab text.
	 */
	this.addPane = function(pane, text) {
		this.tabBar.addItem(text);
		pane.setStyle('position', 'absolute');
		pane.hide();
		this.panes.push(pane);
		if (1 == this.panes.length) {
			this.selectPane(0);
		}
	}
	
	/**
	 * Gets the content pane for this tab window.
	 * Content window can be used to create tab panes within it.
	 *
	 * @return Window
	 */
	this.getContentPane = function() {
		return this.contentPane;
	}
	
	/**
	 * Gets the tab bar control.
	 *
	 * @return TabBar
	 */
	this.getTabBar = function() {
		return this.tabBar;
	}
	
	/**
	 * Makes pane with specified index active.
	 *
	 * @param i (int) pane index.
	 */
	this.selectPane = function(i) {
		if (this.selectedPane) {
			this.selectedPane.hide();
			delete this.selectedPane;
		}
		
		this.tabBar.selectItem(i);
		
		if (this.panes[i]) {
			this.selectedPane = this.panes[i];
			this.panes[i].show();
		}
	}
	
	/**
	 * Gets the selected tab pane.
	 *
	 * @return Window
	 */
	this.getSelectedPane = function() {
		return this.selectedPane;
	}
	
	/**
	 * @protected
	 * Updates tab window layout.
	 */
	this.updateLayout = function() {
		// TODO: implement
	}
	
	this.onTabSelected = function(tabBar) {
		this.selectPane(tabBar.getSelectedItem());
	}
}
